<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Tournament_event_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
	}
	
	function event_category_submit(){
		if(!isset($_POST['id'])){
			$newId = $this->event_category_tournament_entity->createNewEntity($_POST['tournament_id']);
			
			$EVENT_CATEGORY = $this->event_category_tournament_entity->init('id', $newId);
			$EVENT_CATEGORY->setCategory($_POST['category']);
			$EVENT_CATEGORY->setTournamentId($_POST['tournament_id']);
			$EVENT_CATEGORY->setStatus('living');
			
			$EVENT_CATEGORY->update();
			$tournament_id = $_POST['tournament_id'];
		}else{
			$EVENT_CATEGORY = $this->event_category_tournament_entity->init('id', $_POST['id']);
			$EVENT_CATEGORY->setCategory($_POST['category']);
			$EVENT_CATEGORY->update();
			
			$tournament_id = $EVENT_CATEGORY->getTournamentId();
		}
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo $tournament_id;
		exit();
	}
	
	function event_submit(){
		$tournament_id = $_POST['tournament_id'];
		$TOURNAMENT = $this->tournament_entity->init('id',$tournament_id);
		 
		if(!isset($_POST['id'])){
		
			if(! $this->phpvalidator->is_numeric($_POST['odds']) OR $_POST['odds'] <= 0){
				echo "Odds is not valid.";
				exit();
			}
			$newId = $this->event_tournament_entity->createNewEntity();
			$EVENT = $this->event_tournament_entity->init('id', $newId);
			$EVENT->setName($_POST['name']);
			$EVENT->setTournamentId($tournament_id);
			$EVENT->setStatus('living');
			$EVENT->setTournamentEventCategoryId($_POST['event_category_id']);
			$EVENT->setBetEnable('yes');
			$EVENT->setOdds($_POST['odds']);
			$EVENT->update();
		}else{
			
			if(! $this->phpvalidator->is_numeric($_POST['odds']) OR $_POST['odds'] <= 0){
				echo "Odds is not valid.";
				exit();
			}
			
			$EVENT = $this->event_tournament_entity->init('id', $_POST['id']);
			$EVENT->setName($_POST['name']);
			$EVENT->setTournamentEventCategoryId($_POST['event_category_id']);
			$EVENT->setOdds($_POST['odds']);
			$EVENT->update();
		}
		$this->session->set_flashdata('success', lang('dataman.success'));
		echo $tournament_id;
		exit();
	}
	
	function add_events_submit(){
		$tournament_id = $_POST['tournament_id'];
		$event_category_id = $_POST['category_id'];
		$TOURNAMENT = $this->tournament_entity->init('id',$tournament_id);
		
		$numberEvents = count($_POST['name']); 
		$pass = true;
		for($i= 0; $i<$numberEvents ; $i++){
			if(! $this->phpvalidator->is_numeric($_POST['odds'][$i]) OR $_POST['odds'][$i] <= 0){
				echo "Odds is not valid.";
				$pass = false;
				exit();
			}
		}
		
		if($pass){
			for($i= 0; $i<$numberEvents ; $i++){
				$newId = $this->event_tournament_entity->createNewEntity();
				$EVENT = $this->event_tournament_entity->init('id', $newId);
				$EVENT->setName($_POST['name'][$i]);
				$EVENT->setTournamentId($tournament_id);
				$EVENT->setStatus('living');
				$EVENT->setTournamentEventCategoryId($event_category_id);
				$EVENT->setBetEnable('yes');
				$EVENT->setOdds($_POST['odds'][$i]);
				$EVENT->update();
			}
			$this->session->set_flashdata('success', lang('dataman.success'));
			echo $tournament_id;
			exit();
		}
	}
	
	function edit_events_submit(){
		$tournament_id = $_POST['tournament_id'];
		$event_category_id = $_POST['category_id'];
		$TOURNAMENT = $this->tournament_entity->init('id',$tournament_id);
		 
		$numberEvents = count($_POST['item_id']); 
		$pass = true;
		for($i= 0; $i<$numberEvents ; $i++){
			if(! $this->phpvalidator->is_numeric($_POST['odds'][$i]) OR $_POST['odds'][$i] <= 0){
				echo "Odds is not valid.";
				$pass = false;
				exit();
			}
			if(! $this->phpvalidator->is_natural($_POST['ordering'][$i]) OR $_POST['ordering'][$i] < 0){
				echo "Order number is not valid.";
				$pass = false;
				exit();
			}
		}
		
		if($pass){
			for($i= 0; $i<$numberEvents ; $i++){
				$EVENT = $this->event_tournament_entity->init('id', $_POST['item_id'][$i]);
				$EVENT->setName($_POST['name'][$i]);
				$EVENT->setTournamentId($tournament_id);
				$EVENT->setStatus('living');
				$EVENT->setTournamentEventCategoryId($event_category_id);
				$EVENT->setBetEnable('yes');
				$EVENT->setOdds($_POST['odds'][$i]);
				$EVENT->setOrdering(intval($_POST['ordering'][$i]));
				$EVENT->update();
			}
			$this->session->set_flashdata('success', lang('dataman.success'));
			echo $tournament_id;
			exit();
		}
	}
	
	
	
	function event_category_open_hide(){
		$tournament_id = $_POST['tournament_id'];
		$category_id = $_POST['category_id'];
		$EVENT_CATEGORY = $this->event_category_tournament_entity->init('id', $category_id);
		$value = $EVENT_CATEGORY->getIsOpen() == 'yes' ? 'no' : 'yes';
		$EVENT_CATEGORY->setIsOpen($value);
		$EVENT_CATEGORY->update();
		echo 'ok';
	}
	
	function event_change_bet_status(){
		$EVENT = $this->event_tournament_entity->init('id', $_POST['id']);
		if($EVENT->getBetEnable() == 'yes'){
			$EVENT->setBetEnable('no');
		}else{
			$EVENT->setBetEnable('yes');
		}
		$EVENT->update();
		echo "ok";
		exit();
	}
	
	function set_event_result(){
		$EVENT = $this->event_tournament_entity->init('id', $_POST['id']);
		$EVENT->setResult($_POST['result']);
		$EVENT->update();
		echo $EVENT->getMatchId();
		exit;
	}
	
	function update_ordering_category(){
		$tournament_id = $_POST['tournament_id'];
		$id_arr = array_filter( explode(',', $_POST['ids']) );
		$i=count($id_arr)+1;
		foreach($id_arr as $id){
			$this->db->where(array('tournament_id'=>$tournament_id, 'id'=>$id))->update(TBL_CATEGORY_TOURNAMENT, array('ordering'=>$i--));
		}
		echo 'ok';exit();
	}
	
	function event_cancel(){
		$EVENT = $this->event_tournament_entity->init('id',$_POST['event_id']);
	 
		$listBet = $this->bet_tournament_entity->getListUserBetEvent($EVENT->getId());
		foreach($listBet as $obj){
			$BET = $this->bet_tournament_entity->init('id', $obj->id);
			
			$BET->setStatus('cancelled');
			$BET->setResult('cancelled');
			$BET->update();
			
			$BET->processUserBetResult();
		}
		$EVENT->setStatus('cancelled');
		$EVENT->update();
		
		echo 'ok#'.$EVENT->getTournamentId();
		exit();
	}
	
	function event_finish(){
		$EVENT = $this->event_tournament_entity->init('id',$_POST['event_id']);
	 
		if( $EVENT->getResult() == '' ){
			echo "Required set result of event firstly.";exit();
		}
		
		$listBet = $this->bet_tournament_entity->getListUserBetEvent($EVENT->getId());
		foreach($listBet as $obj){
			$BET = $this->bet_tournament_entity->init('id', $obj->id);
			
			$BET->setStatus('finished');
			$BET->setResult($EVENT->getResult());
			$BET->update();
			
			$BET->processUserBetResult();
		}
		$EVENT->setStatus('closed');
		$EVENT->update();
		
		echo 'ok#'.$EVENT->getTournamentId();
		exit();
	}
	
	function event_category_cancel(){
		$EVENT_CATEGORY = $this->event_category_tournament_entity->init('id', $_POST['event_category_id']);
		 
		$listEventLiving = $this->event_tournament_entity->getLivingEventOfCategory($EVENT_CATEGORY->getId());
		foreach($listEventLiving as $item){
			$EVENT = $this->event_tournament_entity->init('id', $item->id);
			
			$listBet = $this->bet_tournament_entity->getListUserBetEvent($EVENT->getId());
			foreach($listBet as $obj){
				$BET = $this->bet_tournament_entity->init('id', $obj->id);
				
				$BET->setStatus('cancelled');
				$BET->setResult('cancelled');
				$BET->update();
				
				$BET->processUserBetResult();
			}
			$EVENT->setStatus('cancelled');
			$EVENT->update();
		}
		$EVENT_CATEGORY->setStatus('cancelled');
		$EVENT_CATEGORY->update();
		
		echo 'ok#'.$EVENT_CATEGORY->getTournamentId();
		exit();
	}
	
	function event_category_finish(){
		$EVENT_CATEGORY = $this->event_category_tournament_entity->init('id', $_POST['event_category_id']);
		$listEventLiving = $this->event_tournament_entity->getLivingEventOfCategory($EVENT_CATEGORY->getId());
		$is_finish = true;
		
		foreach($listEventLiving as $item){
			$EVENT = $this->event_tournament_entity->init('id', $item->id);
			if( $EVENT->getResult() == '' ){
				$is_finish = false;
				break;
			}
		}
		
		if(!$is_finish){
			echo "Required set result of events firstly.";exit();
		}
		
		foreach($listEventLiving as $item){
			$EVENT = $this->event_tournament_entity->init('id', $item->id);
			$listBet = $this->bet_tournament_entity->getListUserBetEvent($EVENT->getId());
			foreach($listBet as $obj){
				$BET = $this->bet_entity->init('id', $obj->id);
				
				$BET->setStatus('finished');
				$BET->setResult($EVENT->getResult());
				$BET->update();
				
				$BET->processUserBetResult();
			}
			$EVENT->setStatus('closed');
			$EVENT->update();
		}
		$EVENT_CATEGORY->setStatus('closed');
		$EVENT_CATEGORY->update();		
		
		echo 'ok#'.$EVENT_CATEGORY->getTournamentId();
	}
	
//end class	
}	